package com.smec.moblie.location;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import com.google.gson.Gson;
import com.smec.moblie.util.WebServiceUtil;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CacheService extends Service {
	public static final String ACTION = "com.smec.remes.mobile.centre.test.CacheService";
	ExecutorService e = Executors.newCachedThreadPool();
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 1) {
				SharedPreferences sharedPreferences = getSharedPreferences("cacheSharedPreferences", Activity.MODE_PRIVATE);
				String cache = sharedPreferences.getString("cache", "");
				System.out.println("............cache == " + cache + "...............");
				String cacheDetail = sharedPreferences.getString("cacheDetail", "");
				System.out.println("............cacheDetail == " + cacheDetail + "...............");
				stopSelf();
			}
		};
	};

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	@Deprecated
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		// e.execute(new mThreadList());
		e.execute(new mThreadDetail());
	}

	public class mThreadList implements Runnable {

		@Override
		public void run() {
			System.out.println("............开始请求数据...............");
			HashMap<String, Object> hashMap = new HashMap<String, Object>();
			hashMap.put("username", "f170080");
			hashMap.put("list_code", "0");
			hashMap.put("date_from", "201501");
			hashMap.put("date_to", "201511");
			hashMap.put("today", "20151118");
			hashMap.put("last_workorderid", "0");
			hashMap.put("isfirstget", "1");
			hashMap.put("get_item_num", "1000");
			Gson gson = new Gson();
			String jsonData = gson.toJson(hashMap);
			String jsonStr = callWebservicejson("GET_MAINTENANCE_LIST", jsonData);
			if (jsonStr != null && !jsonStr.equals("")) {
				System.out.println("............" + jsonStr + "...............");
				ArrayList<LocationBean> resultList = new ArrayList<LocationBean>();
				Locationlist tl = null;
				tl = gson.fromJson(jsonStr, Locationlist.class);
				resultList.addAll(tl.DetailList);

				SharedPreferences mySharedPreferences = getSharedPreferences("cacheSharedPreferences", Activity.MODE_PRIVATE);
				SharedPreferences.Editor editor = mySharedPreferences.edit();
				editor.putString("cache", jsonStr);
				editor.commit();

				System.out.println("............请求数据结束,共" + resultList.size() + "条...............");
				System.out.println("............请求数据结束关闭service...............");
				handler.sendEmptyMessage(1);
			} else {
				System.out.println("............请求数据失败重新请求...............");
				e.execute(new mThreadList());
			}
		}
	}

	public class mThreadDetail implements Runnable {

		@Override
		public void run() {
			HashMap<String, Object> hashMap = new HashMap<String, Object>();
			hashMap.put("username", "demo_user1");
			Gson gson = new Gson();
			String jsonData = gson.toJson(hashMap);
			String jsonStr = callWebservicejson("GET_MAINTENANCE_DETAIL", jsonData);
			System.out.println("............mThreadDetail" + jsonStr + "...............");
			if (jsonStr != null && !jsonStr.equals("")) {
				SharedPreferences mySharedPreferences = getSharedPreferences("cacheSharedPreferences", Activity.MODE_PRIVATE);
				SharedPreferences.Editor editor = mySharedPreferences.edit();
				editor.putString("cacheDetail", jsonStr);
				editor.commit();
				handler.sendEmptyMessage(1);
			} else {
				System.out.println("............请求数据失败重新请求...............");
				e.execute(new mThreadDetail());
			}
		}
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return super.onStartCommand(intent, flags, startId);
	}

	private String callWebservicejson(String type, String p_parameters) {
		String[] keys = new String[] { "p_data_type", "p_parameters" };
		String[] values = new String[] { type, p_parameters };

		// String ret = WebServiceUtil
		// .callWebservice("http://bpmpublic.smec-cn.com:8011/OpenService/GetClobDataByTypeOpenPS?WSDL",
		// "http://www.smec-cn.com/inst", "GetClobDataByType", keys, values,
		// "n0");
		String ret = "";
		try {
			ret = WebServiceUtil.callWebservice("http://bpmdev.smec-cn.com:8011/Inst_Service/ProxyServices/GetClobDataByTypePS?wsdl", "http://www.smec-cn.com/inst", "GetClobDataByType", keys,
					values, "n0");
		} catch (IOException e1) {
			e1.printStackTrace();
		} catch (XmlPullParserException e1) {
			e1.printStackTrace();
		}

		return ret;
	}

}
